package com.yidu.hbatsft.action;


import com.yidu.hbatsft.dao.AtsRivalMapper;
import com.yidu.hbatsft.domain.AtsEmpCoach;
import com.yidu.hbatsft.domain.AtsRival;
import com.yidu.hbatsft.service.AtsEmpCoachService;
import com.yidu.hbatsft.service.RivalSerice;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.vo.UserVo;
import com.yidu.utils.Message;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/RivalAction")
public class Rival {

    @Resource
    RivalSerice rivalSerice ;


    @Resource
    AtsEmpCoachService empCoachService;


    private String creatTimes;

    private String creatTimea;

    /**
     * 查询全部
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, String rivName, String creatTimes, String creatTimea, AtsEmpCoach atsEmpCoach, String type, String id)throws Exception{

        //查询总行数
        int count = rivalSerice.count(rivName);

        System.out.println("cortorller"+pageNum +"   "+pageSize);


         int i=1;

        if(type.equals("教练") || type.equals("null")){
            i=1;
        }else if(type.equals("领导")){
            i=2;
            //atsEmpCoach.setCoachId(2);
        }else if(type.equals("运动员")){
            i=4;
           //atsEmpCoach.setCoachId(4);
        }

        List<AtsRival> list = rivalSerice.findAll(pageNum,pageSize,rivName,creatTimes,creatTimea,i,id);



        Map<String, Object> map=new HashMap<String, Object>();


        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);

        return map;
    }



    /**
     * 新增
     * @param as
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Message insert(AtsRival as, HttpServletRequest request){
        HttpSession session = request.getSession();
        UserVo user = (UserVo) session.getAttribute("manager");
        AtsEmpCoach empCoach = empCoachService.findById(user.getUsWaiid());
        as.setSportid(Integer.valueOf(empCoach.getExpertSpecia()));
        rivalSerice.insert(as);
        Message m = new Message();
        m.setNum(1);

        return  m;
    }


    /**
     * 修改
     * @param as
     * @return
     */
    @RequestMapping("addOrupdate")
    @ResponseBody
    public Message updateByPrimaryKey(AtsRival as){
        rivalSerice.updateByPrimaryKey(as);
        Message m = new Message();

        m.setNum(1);

        return m;
    }

    /**
     * 删除
     * @param rivId
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Message delete(Integer rivId){
        System.out.println("=========="+rivId);
        rivalSerice.delete(rivId);
        Message m = new Message();
        m.setNum(1);
        return m;
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public Message deleteAll(String ids) {
        Message bm = new Message();
        bm.setNum(1);
        rivalSerice.deleteAll(ids);
        bm.setMsg("删除成功!");
        return bm ;
    }


    /**
     * 根据id查询
     * @param
     * @return
     */
    @RequestMapping("findbyid")
    @ResponseBody
    public AtsRival findbyid(Integer rivId){

        return rivalSerice.findbyid(rivId);
    }


    /**
     *
     */
    @RequestMapping("selectBy")
    @ResponseBody
    public AtsRival selectByPrimaryKey(Integer rivId){

        return rivalSerice.selectByPrimaryKey(rivId);
    }


    /**
     * 根据日期查询
     * @param atsRival
     * @return
     */

    @RequestMapping("selectAllt")
    @ResponseBody
    public List<AtsRival> selectAlt(AtsRival atsRival){




        List<AtsRival> list = rivalSerice.selectAllt(atsRival.getCreatTime());

        return list;

    }



    /**
     * 查询全部信息，不分页
     * @return
     */
    @RequestMapping("option")
    @ResponseBody
    public List<AtsRival> selectAll(AtsRival atsRival){


        System.out.println("option");

        List<AtsRival> list = rivalSerice.option(atsRival);

        return list;

    }


}
